<?
/**
*  报表数据处理
*  2014/9/18
*/

include("database.php");

include("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$table_name = "crm_order";
$action = $_GET['action'];

$count_sql_str = "count(id) as cnt"; // 定单数
$count_num_suffix = "个";
if($_GET['count_type'] == 2) {
	$count_sql_str = "sum(total) as cnt"; // 消费金额
	$count_num_suffix = "元";
}

if($action == "source_report") {  // 定单来源
	$sql = "select $count_sql_str, order_source from $table_name where 1=1 group by order_source";
	$sql = add_condition($sql);
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$dic_data['order_source'][$db->f("order_source")]."','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	print_json("【定单来源-总计($total){$count_num_suffix}】", "A", "B", $data);
}
else if($action == "area_report") {  // 区域分布
	$sql = "select $count_sql_str, area from $table_name where 1=1 group by area";
	$sql = add_condition($sql);
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$db->f("area")."','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	print_json("【区域分布-总计($total){$count_num_suffix}】", "A", "B", $data);
} 
else if($action == "month_report") { // 月份定单统计
	$sql = "select $count_sql_str, MONTH(post_time) as m, DATE(post_time) as ymd from $table_name where 1=1 
	group by YEAR(post_time), MONTH(post_time) ORDER BY ymd DESC LIMIT 0,12";
	$sql = add_condition($sql);
	$db->query($sql);
	$total = 0;
	$data = array();
	while($db->nextRecord()) {
		$data[] = "{'label':'".$db->f("m")."','value':'".$db->f("cnt")."', 'link':'j-get_day_report-".$db->f("ymd")."'}";
		$total += $db->f("cnt");
	}
	print_json("【按月定单-总计($total){$count_num_suffix}】", "月份", "定单数", $data);
}
else if($action == "day_report") { // 天数定单统计
	$dt = $_GET['dt'];
	$sql = "select $count_sql_str, DAY(post_time) as d from $table_name where 1=1 and YEAR(post_time)=YEAR('$dt') and MONTH(post_time)=MONTH('$dt') group by DAY(post_time)";
	$sql = add_condition($sql);
	//echo $sql;
	$db->query($sql);
	$total = 0;
	while($db->moveNext()) {
		$data[] = "{'label':'".$db->f("d")."','value':'".$db->f("cnt")."'}";
		$total += $db->f("cnt");
	}
	$arr = split("-", $dt);
	print_json("【按天定单-总计($total){$count_num_suffix}】", "日期-(".$arr[0]."年".$arr[1]."月)", "定单数", $data);	
}

function add_condition($src_sql) {
	//$condition = "order_status<>-1";
	$condition = "1=1";
	if(isset($_GET['date_start'])) {
		$condition .= " and post_time > '".$_GET['date_start']."'"; 
	}
	if(isset($_GET['date_end'])) {
		$condition .= " and post_time < '".$_GET['date_end']."'";
	}
	$src_sql = str_replace("1=1", $condition, $src_sql);
	return $src_sql;
}


function print_json($title, $xname, $yname, $data) {
	echo "{";
	echo "'chart': {";
	echo "'caption' : '$title',";
	echo "'xAxisName' : '$xname',";
	echo "'yAxisName' : '$yname',";
	echo "'formatNumberScale' : '0',";
	if($xname == "CHANNEL")  {
		// for Bar2D (通道统计明细)
		//---------------------------------
		echo "'alternateVGridColor' : 'AFD8F8',";
		echo "'toolTipBorderColor' : '114B78',";
		echo "'baseFontColor' : '114B78',";
		echo "'toolTipBgColor' : 'E7EFF6',";
		echo "'plotBorderDashed' : '1',";
		echo "'plotBorderDashLen' : '2',";
		echo "'useRoundEdges' : '1',";   
		echo "'plotBorderDashGap' : '2',"; 
		echo "'showBorder' : '0',";   
		echo "'bgColor' : 'FFFFFF,FFFFFF',";   
		//---------------------------------
	}
	echo "'baseFontSize' : '12'},";        
	echo "'data' : [";               
	echo join(", ", $data);
	echo "]";
	echo "}";
}


